package com.wsoft.manager;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.TsCodeRulesDataEntity;
import com.wsoft.mapper.TsCodeRulesDataMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/10/16 13:24
 */
@Service
public class TsCodeRulesDataManager extends ServiceImpl<TsCodeRulesDataMapper, TsCodeRulesDataEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据code 和 rulesJson 查询
     * @param code
     * @param rulesJson
     * @return
     */
    public TsCodeRulesDataEntity getByRulesJson(String code,String rulesJson,Integer version){
        if(StrUtil.isBlank(rulesJson) || StrUtil.isBlank(code) || ObjUtil.isEmpty(version)){
            return null;
        }
        QueryWrapper<TsCodeRulesDataEntity> query = new QueryWrapper<>();
        query.lambda().eq(StrUtil.isNotBlank(rulesJson),TsCodeRulesDataEntity::getRulesJson, rulesJson)
                .eq(TsCodeRulesDataEntity::getVersion,version)
                .eq(TsCodeRulesDataEntity::getCode,code);
         return getOne(query);
    }

    /**
     * 根据code 和 version  匹配 rulesJson
     * @param code
     * @param version
     * @return
     */
    public List<TsCodeRulesDataEntity> getByRulesJsonList(String code, Integer version){
        if( StrUtil.isBlank(code)){
            return null;
        }
        QueryWrapper<TsCodeRulesDataEntity> query = new QueryWrapper<>();
        query.lambda()
                .eq(ObjUtil.isNotEmpty(version),TsCodeRulesDataEntity::getVersion,version)
                .eq(TsCodeRulesDataEntity::getCode,code);
        return list(query);
    }

    /**
     * 根据以下参数获取,用于判断 使用自定义类的时候 是否存在该data数据
     * @param code
     * @param jsonObject
     * @param version
     * @return
     */
    public TsCodeRulesDataEntity getClassValue(String code,String jsonObject,Integer version){
        QueryWrapper<TsCodeRulesDataEntity> query = new QueryWrapper<>();
        query.lambda()
                .eq(TsCodeRulesDataEntity::getVersion,version)
                .eq(TsCodeRulesDataEntity::getParamObject,jsonObject)
                .eq(TsCodeRulesDataEntity::getCode,code);
        return getOne(query);
    }
}
